<template>
  <div class="container">
    <div class="header">
      <div class="left">
        <a-tooltip placement="bottom" @click="handleClick">
          <template #title>加粗</template>
          <bold-outlined/>
        </a-tooltip>
        <a-tooltip placement="bottom">
          <template #title>斜体</template>
          <italic-outlined/>
        </a-tooltip>
        <a-tooltip placement="bottom">
          <template #title>下划线</template>
          <underline-outlined/>
        </a-tooltip>
        <a-tooltip placement="bottom">
          <template #title>插入图片</template>
          <file-image-outlined/>
        </a-tooltip>
        <a-tooltip placement="bottom">
          <template #title>有序列表</template>
          <ordered-list-outlined/>
        </a-tooltip>
        <a-tooltip placement="bottom">
          <template #title>无序列表</template>
          <unordered-list-outlined/>
        </a-tooltip>
        <a-tooltip placement="bottom">
          <template #title>插入表格</template>
          <table-outlined/>
        </a-tooltip>
        <a-tooltip placement="bottom">
          <template #title>插入链接</template>
          <link-outlined/>
        </a-tooltip>
        <a-dropdown>
          <span class="title_dropdown"  @click.prevent>
            H
          </span>
          <template #overlay>
            <a-menu>
              <a-menu-item v-for="item in titleList" :key="item">
                <span>H{{item}}</span>
              </a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </div>
      <div class="right">
        <a-tooltip placement="bottom">
          <template #title>预览</template>
          <eye-outlined @click="handlePreview"/>
        </a-tooltip>
      </div>
    </div>
    <main>
      <textarea ref="content"  placeholder="请输入内容..." class="content" v-model="text"></textarea>
      <div ref="preview"  v-show="isPreview" v-highlight class="preview" id="content"></div>
    </main>
  </div>
</template>

<script setup lang="ts">
import {computed, ref, watch} from "vue";
import {
  BoldOutlined,
  EyeOutlined,
  FileImageOutlined,
  ItalicOutlined,
  LinkOutlined,
  OrderedListOutlined,
  TableOutlined,
  UnderlineOutlined,
  UnorderedListOutlined,
} from '@ant-design/icons-vue';
import { marked } from 'marked';
const  props =   defineProps(['values'])
const emit = defineEmits(['update:values'])
const content = ref<HTMLTextAreaElement | null>(null);
const preview = ref<HTMLDivElement | null>(null);
const isPreview = ref<boolean>(false);
const titleList = ['1','2','3','4','5','6'];
const text = ref(props.values)

watch(
    () => props.values,
    (newVal) => {
      text.value = newVal;
    }
);
/**
 * 获取选中的文字
 */
const SelectedText = () => {
  const element = content.value;
  if (!element) return;
  return element.value.substring(
      element.selectionStart,
      element.selectionEnd
  );
};
// 获取当前光标位置
const getCursorPosition = (): number => {
  if (content.value) {
    return content.value.selectionStart;
  }
  return 0;
};

// 在当前光标位置插入文本
const insertText = (text: string) => {
  if (content.value) {
    const cursorPosition = getCursorPosition();
    const currentValue = content.value.value;
    content.value.value = `${currentValue.slice(0, cursorPosition)}${text}${currentValue.slice(cursorPosition)}`;
  }
};
// 预览
function handlePreview() {
  if(!preview.value) return;
  isPreview.value = !isPreview.value;
  if(isPreview.value) {
    preview.value.innerHTML = marked(text.value)
  }
}
watch(text,()=>{
  if(!preview.value) return;
  preview.value.innerHTML = marked(text.value)
})
// 处理按钮点击事件
const handleClick = () => {
  insertText('**加粗的文本**');
};

watch(text,()=>{
  emit('update:values',text.value);
})


</script>

<style scoped lang="less">
.container {
  width: 100%;
  border-radius: 3px;
  height: 200px;
  box-sizing: border-box;
  border: 1px solid #dddddd;
  .header {
    border-bottom: 1px solid #dddddd;
    padding: 15px 15px;
    align-items: center;
    display: flex;
    justify-content: space-between;
    .anticon {
      font-size: 15px;
      margin: 0 10px;
      cursor: pointer;
    }
    .title_dropdown {
      color: #262626;
      font-weight: 600;
    }
  }
  main {
    display: flex;
    height: 90%;
    .content {
      border: 0;
      resize: none;
      outline: none;
      flex: 5;
      height: 100%;
      padding: 20px;
      font-family: "JetBrainsMono";
      overflow: overlay;
      &::-webkit-scrollbar {
        width: 0;
        height: 0;
        background: transparent;
      }
      &::-webkit-scrollbar-track {
        background: white;
      }
      &::-webkit-scrollbar-thumb {
        background-color: #999;
        border-radius: 20px;
        border: 2px solid #eee;
      }
      &:hover::-webkit-scrollbar {
        width: 8px;
      }
    }
    .preview {
      position: relative;
      top: 0;
      flex: 5;
      width: 100%;
      height: 100%;
      border-left: 1px solid #dddddd;
      padding: 20px;
      background: white;
      overflow: overlay;
      &::-webkit-scrollbar {

        width: 0;
        height: 0;
        background: transparent;
      }
      &::-webkit-scrollbar-track {
        background: white;
      }
      &::-webkit-scrollbar-thumb {
        background-color: #999;
        border-radius: 20px;
        border: 2px solid #eee;
      }
      &:hover::-webkit-scrollbar {

        width: 8px;
      }
    }
  }
}
</style>